#include "TcpServer.h"

TcpServer::TcpServer(int port, QWidget* parent)
{
	this->port = port;
	lb_port = new QLabel(QString::number(port));
	initGUI();
	initServer(port);
	connect(btn_send, &QPushButton::clicked, this, &TcpServer::On_Send);
	
}

TcpServer::~TcpServer()
{
	for each (QTcpSocket * s in list_socket)
	{
		s->close();
	}
}

void TcpServer::initServer(int port)
{
	server = new QTcpServer(this);
	server->listen(QHostAddress::Any, port);
	connect(server, &QTcpServer::newConnection, this, &TcpServer::OnConnected);
	btn_start->setEnabled(false);
}

void TcpServer::initGUI()
{
	/* 初始化GroupBox */
	mainLayout = new QGridLayout(this);
	top_layout = new QGridLayout();
	center_layout = new QGridLayout();
	bottom_layout = new QGridLayout();
	topBox = new QGroupBox("Socket状态");
	centerBox = new QGroupBox("数据接收与提示窗口");
	bottomBox = new QGroupBox("数据发送窗口");
	topBox->setLayout(top_layout);
	centerBox->setLayout(center_layout);
	bottomBox->setLayout(bottom_layout);
	mainLayout->addWidget(topBox);
	mainLayout->addWidget(centerBox);
	mainLayout->addWidget(bottomBox);
	mainLayout->setRowStretch(1, 1);
	mainLayout->setRowStretch(2, 1);

	/* 初始化控件 */
	lb_status = new QLabel("已启动,等待连接...");//记录Socket状态
	btn_start = new QPushButton("开始监听");
	btn_stop = new QPushButton("停止监听");
	lb_numclient = new QLabel("0");
	connect(btn_stop, &QPushButton::clicked, this, &TcpServer::On_Stop);
	connect(btn_start, &QPushButton::clicked, this, &TcpServer::On_Start);
	top_layout->addWidget(lb_status, 0, 0);
	top_layout->addWidget(btn_start, 1, 0);
	top_layout->addWidget(btn_stop, 1, 1);
	top_layout->addWidget(new QLabel("监听端口："), 1, 3);
	top_layout->addWidget(lb_port,1,4);

	top_layout->addWidget(new QLabel("客户端数："),0,3);
	top_layout->addWidget(lb_numclient, 0, 4);
	top_layout->setColumnStretch(2, 1);
	top_layout->setSpacing(10);
	

	t_receive = new QTextEdit();
	t_receive->setReadOnly(true);
	center_layout->addWidget(t_receive);

	t_send = new QTextEdit();
	btn_send = new QPushButton("发送");
	bottom_layout->addWidget(t_send,0,0,2,1);
	bottom_layout->addWidget(btn_send, 0, 1);
	bottom_layout->setRowStretch(1, 1);

}

void TcpServer::OnConnected()
{
	QTcpSocket* socket = server->nextPendingConnection();
	list_socket.append(socket);
	connect(socket, &QTcpSocket::readyRead, [=]() {
		QByteArray str = socket->readAll();
		t_receive->append(socket->peerAddress().toString().split(":")[3] + ":" + QString::number(socket->peerPort()) +"【接收】: "+ str);
		});
	connect(socket, &QTcpSocket::disconnected, [=]()
		{
			list_socket.removeOne(socket);
			if(list_socket.isEmpty()) lb_status->setText("已启动,等待连接...");
			lb_numclient->setText(QString::number(list_socket.count()));
			t_receive->append(socket->peerAddress().toString().split(":")[3] + ":" + QString::number(socket->peerPort()) + "【错误】: " + "断开连接!");
			socket->deleteLater();
		});
	lb_status->setText("已连接");
	lb_numclient->setText(QString::number(list_socket.count()));
}

void TcpServer::On_Send()
{
	for each (QTcpSocket* s in list_socket)
	{
		s->write(t_send->toPlainText().toUtf8());
	}
	t_receive->append("服务器[ " + QString::number(port) + " ]【发送】: " + t_send->toPlainText().toUtf8());
}

void TcpServer::On_Stop()
{
	for each (QTcpSocket * s in list_socket)
	{
		s->close();
		s->deleteLater();
	}
	list_socket.clear();
	server->close();
	btn_start->setEnabled(true);
	btn_stop->setEnabled(false);
	lb_status->setText("已关闭");
}

void TcpServer::On_Start()
{
	lb_status->setText("已启动,等待连接...");
	server->listen(QHostAddress::Any, port);
	btn_start->setEnabled(false);
	btn_stop->setEnabled(true);
}


